using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
 

namespace BumbleGame
{
    class Menu
    {
        private Texture2D mainMenuBackground, statsMenuBackground, dressupMenuBackground;
        private AnimatedTexture mainMenuButtonStats, mainMenuButtonStart, mainMenuButtonDressup;
        private Rectangle mainMenuRectangle, statsMenuRectangle, dressupMenuRectangle;
        private bool onDressupMenu, transition;
        public bool onStatsMenu, onMainMenu; 

        public Menu()
        {
            onMainMenu = true;
            onStatsMenu = false;
            onDressupMenu = false;
            transition = true;
        }
        public void LoadMenu(GraphicsDeviceManager graphics, ContentManager content, Viewport viewport)
        {
            mainMenuButtonStats = new AnimatedTexture();
            mainMenuButtonStart = new AnimatedTexture();
            mainMenuButtonDressup = new AnimatedTexture();

            //load the textures for use in all menus
            mainMenuBackground = content.Load<Texture2D>("images/menu/Title");
            statsMenuBackground = content.Load<Texture2D>("images/menu/stats_test");
            dressupMenuBackground = content.Load<Texture2D>("images/menu/dressup_select");

            mainMenuButtonStats.Load(graphics.GraphicsDevice, content, "Images/menu/stats_select", 1, 1, 1, 0, 0, false);
            mainMenuButtonStats.SpritePosition = new Vector2(viewport.Width / 2, viewport.Height / 2);
            mainMenuButtonStart.Load(graphics.GraphicsDevice, content, "Images/menu/start_select", 1, 1, 1, 0, 0, false);
            mainMenuButtonStart.SpritePosition = new Vector2(viewport.Width / 2, viewport.Height / 2);
            mainMenuButtonDressup.Load(graphics.GraphicsDevice, content, "Images/menu/dressup_select", 1, 1, 1, 0, 0, false);
            mainMenuButtonDressup.SpritePosition = new Vector2(viewport.Width / 2, viewport.Height / 2);

            mainMenuRectangle = new Rectangle(0, 0, mainMenuBackground.Width, mainMenuBackground.Height);
            statsMenuRectangle = new Rectangle(0, 0, statsMenuBackground.Width, statsMenuBackground.Height);
            dressupMenuRectangle = new Rectangle(0, 0, dressupMenuBackground.Width, dressupMenuBackground.Height);
        }

        public void UpdateMenu(ref int selection, ref int maxSelection, ref bool selected, ref int gameState, ref bool stateTransition, ref bool menuBack, ref tagScreenFade ScreenFade, ref float musicVolume)
        {
            if (onMainMenu)
            {
                if (transition)
                {
                    //which button do you start on, and how many buttons are there to scroll between?
                    selection = 1;
                    maxSelection = 2;                   
                    transition = false;
                }                
                //if player has selected a button, do what they want
                if (selected)
                {
                    selected = false;

                    switch (selection)
                    {
                        case 0: //selecting the stats menu
                            onStatsMenu = true;
                            onMainMenu = false;
                            transition = true;
                            break;

                        case 1: //starting the game
                            ScreenFade.Begin(Screen.Game, FadeType.FadeOut, 1.0f, musicVolume);
                            //gameState = 1;
                            //stateTransition = true;                            
                            break;

                        case 2: //selecting the dressup menu
                            onDressupMenu = true;
                            onMainMenu = false;
                            transition = true;
                            break;
                    }
                }

            }

            if (onStatsMenu)
            {
                if (transition)
                {   //nothing here yet, update later when the menu is more complete
                    selection = 0;
                    maxSelection = 0;                    
                    transition = false;
                }
                if (menuBack)
                {
                    menuBack = false;
                    onStatsMenu = false;
                    onMainMenu = true;
                    transition = true;
                }
                if (selected)
                {
                    selected = false;
                    //for now just go back to the main menu
                    //onStatsMenu = false;
                    //onMainMenu = true;
                    transition = true;
                    //if there are more options, use this switch to dicide what to do
                    /*switch (selection)
                    {
                        case 0:
                            onStatsMenu = false;
                            onMainMenu = true;
                            transition = true;
                            break;

                        case 1:
                            gameState = 1;
                            break;

                        case 2:
                            onDressupMenu = true;
                            onMainMenu = false;
                            break;
                    }*/
                }
            }
            
            if (onDressupMenu)
            {
                if (transition)
                {   //nothing here yet, update later when the menu is more complete
                    selection = 0;
                    maxSelection = 0;                   
                    transition = false;
                }
                if (menuBack)
                {
                    menuBack = false;
                    onDressupMenu = false;
                    onMainMenu = true;
                    transition = true;
                }
                if (selected)
                {
                    selected = false;                                  
                    //if there are more options, use this switch to dicide what to do
                   /* switch (selection)
                    {
                        case 0:
                            onDressupMenu = false;
                            onMainMenu = true;
                            transition = true;
                            break;

                        case 1:
                            gameState = 1;
                            break;

                        case 2:
                            onDressupMenu = true;
                            onMainMenu = false;
                            break;
                    }*/
                }
            }
        }               

        public void DrawMenu(SpriteBatch batch, int selection)
        {
            if (onMainMenu)
            {
                batch.Draw(mainMenuBackground, mainMenuRectangle, Color.White);

                switch (selection)
                { 
                    case 0:
                        mainMenuButtonStats.DrawFrame(batch);
                        break;
                    case 1:
                        mainMenuButtonStart.DrawFrame(batch);
                        break;
                    case 2:
                        mainMenuButtonDressup.DrawFrame(batch);
                        break;
                }
            }

            if (onStatsMenu)
            {
                batch.Draw(statsMenuBackground, statsMenuRectangle, Color.White);
            }

            if (onDressupMenu)
            {
                batch.Draw(dressupMenuBackground, dressupMenuRectangle, Color.White);
            }
        }
    }
}
